import 'package:get/get.dart';
import '../../data/services/lunar_service.dart';
import 'tools_state.dart';

/// 工具逻辑控制器 - Tools Logic Controller
/// 
/// 负责工具页面的业务逻辑处理，包括工具分类、工具列表、收藏管理等
/// Handles tools page business logic including tool categories, tool lists, favorite management, etc.
/// 
/// 基于GetX最新最佳实践实现，提供响应式状态管理
/// Based on GetX latest best practices, providing reactive state management
class ToolsLogic extends GetxController {
  /// 状态管理 - State management
  final state = Rx<ToolsState>(ToolsState());

  @override
  void onInit() {
    super.onInit();
    // 初始化状态 - Initialize state
    _initializeState();
  }

  @override
  void onReady() {
    super.onReady();
    // 页面准备完成后加载数据 - Load data after page is ready
    loadLunarInfo();
  }

  /// 初始化状态 - Initialize state
  /// 设置初始状态值，为数据加载做准备
  /// Sets initial state values, prepares for data loading
  void _initializeState() {
    // 状态已在构造函数中初始化，无需重新创建
    // State is already initialized in constructor, no need to recreate
    state.refresh();
  }

  /// 加载农历信息 - Load lunar information
  /// 获取今日老黄历信息并更新状态
  /// Gets today's lunar calendar information and updates state
  Future<void> loadLunarInfo() async {
    try {
      state.value.isLoading = true;
      state.refresh();

      // 获取今日农历信息 - Get today's lunar information
      final lunarInfo = LunarService.getTodayLunarInfo();
      
      state.value.lunarInfo = lunarInfo;
      state.value.isLoading = false;
      state.refresh();
      
      print('农历信息加载成功: ${lunarInfo['lunar_date']}');
    } catch (e) {
      print('加载农历信息失败: $e');
      state.value.isLoading = false;
      state.refresh();
    }
  }

  /// 刷新数据 - Refresh data
  /// 重新加载农历信息
  /// Reloads lunar information
  Future<void> refreshData() async {
    await loadLunarInfo();
  }
}
